#include "types.h"
#include "stat.h"
#include "user.h"
#include "fcntl.h"

void childRun(char* addr) {
	char* msg = malloc(17);
	strcpy(msg, "child wrote here");
	sleep(100);
	strcpy(addr, msg);
	printf(2, "child : deattaching\n");
	sleep(100);
	shmdt(addr);
	printf(2, "child : deattached\n");
	sleep(100);
	free(msg);
}

void fatherRun(int num, char* addr) {
	wait();
	printf(2, "father : did child write here?\n");
	sleep(100);
	printf(2, "%d \n", addr);
	sleep(100);
	printf(2, "father : deattaching\n");
	sleep(100);
	shmdt(addr);
	printf(2, "father : deattached\n");
	sleep(100);
	shmdel(num);
}

int main(int argc,const char* argv[])
{
	printf(1,"\n\n-------------- Start Swaptest ---------------\n\n");

  int num= shmget(1,4096,CREAT);
  int fid =fork();
  char* addr = (char*)shmat(num,SHM_RDWR);
  if (fid==0)
  {
		childRun(addr);
  }
  else
  {
		fatherRun(num, addr);
  }
	printf(1,"\n\n-------------- End Swaptest ---------------\n\n");

  exit();
  return 0;
}
